﻿using Max.PH4_2102A.Shopping.DAL;
using Max.PH4_2102A.Shopping.Domain;
using Max.PH4_2102A.Shopping.IRespository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH4_2102A.Shopping.Respository
{
    /// <summary>
    /// 品牌的仓储实现
    /// </summary>
    public class BrandRespository : IBrandRespository
    {
        EFCoreHelper _db;
        /// <summary>
        /// 构造函数注入上下文
        /// </summary>
        /// <param name="db"></param>
        public BrandRespository(EFCoreHelper db)
        {
            _db = db;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Delete(BrandModel model)
        {
            _db.Brands.Remove(model);
            int result = _db.SaveChanges();
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public int Delete(List<BrandModel> models)
        {
            //实现批量删除（Range - 删除范围）
            _db.Brands.RemoveRange(models);
            int result = _db.SaveChanges();
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Insert(BrandModel model)
        {
            _db.Brands.Add(model);
            int result = _db.SaveChanges();
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IQueryable<BrandModel> Query()
        {
            var list = _db.Brands;
            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(BrandModel model)
        {
            _db.Brands.Update(model);
            int result = _db.SaveChanges();
            return result;
        }
    }
}
